<?php

require_once 'DataProvider.php';
require_once '../dto/DoctorDto.php';

class DoctorDao {

    public static function GetDoctorByEmailAndPassword($email, $password) {
        $sql = "SELECT * " .
                " FROM doctor WHERE  strcmp(email,'" . $email . "') = 0 and strcmp(password,'" . $password . "') = 0;";

        $resultquery = DataProvider::ExecuteQuery($sql);
        if ($resultquery == null)
            return null;
        $doctor = null;
        while ($row = mysql_fetch_array($resultquery)) {
            $doctor = new DoctorDto();
            $doctor->setEmail($row['email']);
            $doctor->setPassword($row['password']);
            $doctor->setFirstName($row['first_name']);
            $doctor->setLastName($row['last_name']);
        }
        return $doctor;
    }

    public static function GetDoctorByEmail($email) {
        $sql = "SELECT * " .
                " FROM doctor WHERE  strcmp(email,'" . $email . "') = 0 ";

        $resultquery = DataProvider::ExecuteQuery($sql);
        if ($resultquery == null)
            return null;
        $doctor = null;
        while ($row = mysql_fetch_array($resultquery)) {
            $doctor = new DoctorDto();
            $doctor->setEmail($row['email']);
            $doctor->setPassword($row['password']);
            $doctor->setFirstName($row['first_name']);
            $doctor->setLastName($row['last_name']);
        }
        return $doctor;
    }

    public static function LoadAllDoctor() {
        $sql = 'SELECT * FROM doctor ';
        $result = dataprovider::ExecuteQuery($sql);
        $arrDoctors = new ArrayObject();
        while ($row = mysql_fetch_array($result)) {
            $doctor = new DoctorDto();
            $doctor->setEmail($row['email']);
            $doctor->setPassword($row['password']);
            $doctor->setFirstName($row['first_name']);
            $doctor->setLastName($row['last_name']);
            $arrDoctors[] = $doctor;
        }
        return $arrDoctors;
    }

    public static function InsertDoctor(DoctorDto $doctor) {
        $sql = "INSERT INTO doctor (email,password,first_name, last_name) VALUES ('"
                . $doctor->getEmail() . "','"
                . $doctor->getPassword() . "','"
                . $doctor->getFirstName() . "','"
                . $doctor->getLastName() . "')";
        $result = dataprovider::ExecuteQuery($sql);
        if ($result)
            return TRUE;
        else {
            return FALSE;
        }
    }

    public static function UpdateDoctor(DoctorDto $doctor) {
        $sql = "UPDATE Doctor" .
                " SET Password = '" . $doctor->getPassword()
                . "', Email = '" . $doctor->getFirstName
                . "', Age ='" . $doctor->getLastName . "'";
        $result = dataprovider::ExecuteQuery($sql);
        if ($result)
            return TRUE;
        else {
            return FALSE;
        }
    }

    public static function DeleteDoctor($doctorEmail) {
        $sql = "DELETE FROM doctor WHERE email = '"
                . $doctorEmail . "'";
        $result = dataprovider::ExecuteQuery($sql);
        if ($result)
            return TRUE;
        else {
            return FALSE;
        }
    }
    
}

?>
